<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: Attribute.php 2870 2010-05-17 03:04:13Z huuphuoc $
 * @since        2.0.5
 */

class Shop_Models_Dao_Pdo_Mysql_Evaluation extends Tomato_Model_Dao
    implements Shop_Models_Interface_Evaluation
{
    public function convert($entity)
    {
        return new Shop_Models_Evaluation($entity);
    }

    public function getById($exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix.'evaluation'))
                        ;
                if($exp){
                    if(isset($exp['order_id'])){
                        $select->where('e.order_id = ?', $exp['order_id']);
                    }
                    if(isset($exp['seller_user_id'])){
                        $select->where('e.seller_user_id = ?', $exp['seller_user_id']);
                    }
                    if(isset($exp['buyer_user_id'])){
                        $select->where('e.buyer_user_id = ?', $exp['buyer_user_id']);
                    }
                    if(isset($exp['seller_enterprise_id'])){
                        $select->where('e.seller_enterprise_id = ?', $exp['seller_enterprise_id']);
                    }
                    if(isset($exp['buyer_enterprise_id'])){
                        $select->where('e.buyer_enterprise_id = ?', $exp['buyer_enterprise_id']);
                    }
                }
                $select->limit(1);

        $row =  $select->query()->fetchAll();
        $evaluation = new Tomato_Model_RecordSet($row, $this);
        return (count($evaluation) == 0) ? null : $evaluation[0];
    }

    public function add($evaluation){
        return $this->_conn->insert($this->_prefix.'evaluation', array(
                'order_id'                   => $evaluation->order_id,
                'seller_user_id'             => $evaluation->seller_user_id,
                'buyer_user_id'              => $evaluation->buyer_user_id,
                'seller_enterprise_id'       => $evaluation->seller_enterprise_id,
                'buyer_enterprise_id'        => $evaluation->buyer_enterprise_id,
                'seller_to_buyer_feedback'   => $evaluation->seller_to_buyer_feedback,
                'seller_to_buyer_evaluation' => $evaluation->seller_to_buyer_evaluation,
                'buyer_to_seller_feedback'   => $evaluation->buyer_to_seller_feedback,
                'buyer_to_seller_evaluation' => $evaluation->buyer_to_seller_evaluation,
            ));
    }

    public function update($evaluation, $buyer=true){
        $where[] = 'order_id = '.$this->_conn->quote($evaluation->order_id);
        $data = array(
                    'order_id'                   => $evaluation->order_id,
                );
        if($buyer){
            $data['buyer_user_id']              = $evaluation->buyer_user_id;
            $data['buyer_to_seller_feedback']   = $evaluation->buyer_to_seller_feedback;
            $data['buyer_to_seller_evaluation'] = $evaluation->buyer_to_seller_evaluation;
            $data['buyer_enterprise_id']        = $evaluation->buyer_enterprise_id;
        }
        else{
            $data['seller_user_id']             = $evaluation->seller_user_id;
            $data['seller_to_buyer_feedback']   = $evaluation->seller_to_buyer_feedback;
            $data['seller_to_buyer_evaluation'] = $evaluation->seller_to_buyer_evaluation;
            $data['seller_enterprise_id']       = $evaluation->seller_enterprise_id;
        }
        return $this->_conn->update($this->_prefix.'evaluation', $data, $where);
    }

    public function updateEnt($evaluationEnt){
        $sql = "update {$this->_prefix}enterprise set evaluation_point = (evaluation_point*evaluation_counter+{$evaluationEnt['evaluation_point']})/(evaluation_counter+1), evaluation_counter = evaluation_counter+1 where id = {$this->_conn->quote($evaluationEnt['enterprise_id'])}";        
        return $this->_conn->query($sql);
    }

    public function updateCus($evaluationCus){
        $sql = "update {$this->_prefix}customer set evaluation_point = (evaluation_point*evaluation_counter+{$evaluationCus['evaluation_point']})/(evaluation_counter+1), evaluation_counter = evaluation_counter+1 where user_id = {$this->_conn->quote($evaluationCus['customer_id'])}";
        return $this->_conn->query($sql);
    }

    public function getByEU($offset, $count, $exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix.'evaluation'))
                        ;
                if($exp){
                    if(isset($exp['buyer_user_id'])){
                        $select->where('e.buyer_user_id = ?', $exp['buyer_user_id']);
                    }
                    if(isset($exp['seller_enterprise_id'])){
                        $select->where('e.seller_enterprise_id = ?', $exp['seller_enterprise_id']);
                    }
                    if(isset($exp['buyer_enterprise_id'])){
                        $select->where('e.buyer_enterprise_id = ?', $exp['buyer_enterprise_id']);
                    }
                    if(isset($exp['str_buyer_id'])){
                        $select->where("'{$exp['str_buyer_id']}' like concat('%,',e.buyer_user_id,',%')");
                    }
                }
                $select->order('e.feedback_id DESC')
                       ->limit($count, $offset)
                        ;                      
        $rs =  $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function countEU($exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix.'evaluation'), array('num_evaluation' => 'COUNT(*)'))
                        ;
                if($exp){
                    if(isset($exp['buyer_user_id'])){
                        $select->where('e.buyer_user_id = ?', $exp['buyer_user_id']);
                    }
                    if(isset($exp['seller_enterprise_id'])){
                        $select->where('e.seller_enterprise_id = ?', $exp['seller_enterprise_id']);
                    }
                    if(isset($exp['buyer_enterprise_id'])){
                        $select->where('e.buyer_enterprise_id = ?', $exp['buyer_enterprise_id']);
                    }
                    if(isset($exp['str_buyer_id'])){
                        $select->where("'{$exp['str_buyer_id']}' like concat('%,',e.buyer_user_id,',%')");
                    }
                }
        return $select->query()->fetch()->num_evaluation;
    }

    public function sum($exp = null, $buyer = true){
        if($buyer){
            $sum = "sum(e.seller_to_buyer_evaluation)";
        }else{
            $sum = "sum(e.buyer_to_seller_evaluation)";
        }
        $select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix.'evaluation'), array('sum_evaluation' => $sum))
                        ;
                if($exp){
                    if(isset($exp['buyer_user_id'])){
                        $select->where('e.buyer_user_id = ?', $exp['buyer_user_id']);
                    }
                    if(isset($exp['seller_enterprise_id'])){
                        $select->where('e.seller_enterprise_id = ?', $exp['seller_enterprise_id']);
                    }
                    if(isset($exp['buyer_enterprise_id'])){
                        $select->where('e.buyer_enterprise_id = ?', $exp['buyer_enterprise_id']);
                    }
                }
        return $select->query()->fetch()->sum_evaluation;
    }
    
    public function orderEval($exp = null, $offset = 0, $count = 0){
		$select = $this->_conn
                        ->select()
                        ->from(array('o' => $this->_prefix.'shop_order'), array('id' => 'order_id', 'order_invoice', 'order_total_amount', 'order_total_amount_currency', 'enterprise_id'))
                        ->joinInner(array('e' => $this->_prefix.'evaluation'), "o.order_id = e.order_id")
                        ;
                if($exp){
                    if(isset($exp['enterprise_id'])){
                        $select->where('o.enterprise_id = ?', $exp['enterprise_id']);
                    }
                    if(isset($exp['customer_id'])){
                        $select->where("'{$exp['customer_id']}' like concat('%,',o.customer_id,',%')");
                    }
                }
                $select->order('e.feedback_id DESC');
                if($count){
                	$select->limit($count, $offset);
                }
        $rs =  $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);    	
    }
    
    public function counOrderEval($exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('o' => $this->_prefix.'shop_order'), array('num_evaluation' => 'COUNT(*)'))
                        ->joinInner(array('e' => $this->_prefix.'evaluation'), "o.order_id = e.order_id", array())
                        ;
                if($exp){
                    if(isset($exp['enterprise_id'])){
                        $select->where('o.enterprise_id = ?', $exp['enterprise_id']);
                    }
                    if(isset($exp['customer_id'])){                        
                        $select->where("'{$exp['customer_id']}' like concat('%,',o.customer_id,',%')");
                    }
                }               
        return $select->query()->fetch()->num_evaluation;
    }
}

